/**
 * 解决自定义expand导致固定列高度无法撑开问题
 * ps: 暂时只处理了fixed: right 如类似场景 参考样品单管理列表  sample-list/index.vue
 * ps: 页码变更需要手动执行expandFixedRow   sample-list/index.vue
 * @param rowIndex 当前行下标
 */
const expandFixedRow = (rowIndex) => {
    try {
        if (Array.isArray(rowIndex)) {
            rowIndex.forEach(item => {
                setRowStyle(item)
            })
        } else {
            setRowStyle(rowIndex)
        }
    } catch (e) {
        console.warn('处理展开列(fixed)高度自适应报错了，请检查！', e)
    }
}

const setRowStyle = rowIndex => {
    //  fixed --> right
    const tbody = document.querySelector('.vxe-table--body-wrapper.fixed-right--wrapper .vxe-table--body>tbody')
    let rows, cell
    if (tbody) {
        rows = tbody.querySelectorAll('tr')
        cell = rows[rowIndex].querySelector('td')
    }
    //  fixed --> left
    const tbodyLeft = document.querySelector('.vxe-table--body-wrapper.fixed-left--wrapper .vxe-table--body>tbody')
    let rowsLeft, cellLeft
    if (tbodyLeft) {
        rowsLeft = tbodyLeft.querySelectorAll('tr')
        cellLeft = rowsLeft[rowIndex].querySelector('td')
    }
    if (!tbody && !tbodyLeft) return
    let maxCellHeight = 0;
    const originalTbody = document.querySelector('.vxe-table--main-wrapper .vxe-table--body>tbody')
    const originalRows = originalTbody.querySelectorAll('tr')
    const originCells = originalRows[rowIndex].querySelectorAll('td')
    originCells.forEach(row => {
        const cellHeight = row.offsetHeight
        if (cellHeight > maxCellHeight) {
            maxCellHeight = cellHeight
        }
    })
    if (tbody) cell.style.height = `${maxCellHeight}px`
    if (tbodyLeft) cellLeft.style.height = `${maxCellHeight}px`
}

export default {
    expandFixedRow
}